package hc.group;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import jm.easyconfig.CfgContextUser;

@RestController
public class DemoController {
	@Autowired
	DemoService demoService;

	@GetMapping("/service")
	public String service() {
		return demoService.toString();
	}

	@GetMapping("/hello")
	public String hello() {
		String[] names = applicationContext.getBeanDefinitionNames();

		StringBuilder sb = new StringBuilder();
		for (String n : names) {
			sb.append(n + ":" + applicationContext.getBean(n).getClass().getName());
			sb.append("\n");
		}

		return sb.toString();
	}

	@Autowired
	ServletContext servletContext;
	@Autowired
	ApplicationContext applicationContext;

	@GetMapping("/login")
	public String login(String user) {
		servletContext.setAttribute("user", user);
		return applicationContext.getBean(CfgContextUser.class).getUser();
	}
}
